import { LogoutOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons';
import { Avatar, Menu, Spin } from 'antd';
import React from 'react';
import { history, connect, useIntl } from 'umi';
import { avatar } from '@/utils/const';
import HeaderDropdown from '../HeaderDropdown';
import { USER_DASHBOARD_PATH } from '@apulis/constants';
import styles from './index.less';


const AvatarDropdown = (props) => {
  const { formatMessage } = useIntl();
  const {
    currentUser = {
      name: 'User',
    },
    menu,
  } = props;

  const onMenuClick = (event) => {
    const { key } = event;

    if (key === 'logout') {
      const { dispatch } = props;

      if (dispatch) {
        dispatch({
          type: 'login/logout',
        });
      }
    }

    window.open(`${window.location.origin}${USER_DASHBOARD_PATH}/basic/${key === 'setting' ? 'admin/settings' : 'dashboard'}`, '_self');
  };



  const menuHeaderDropdown = (
    <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
      {menu && (
        <>
          <Menu.Item key='center'>
            <UserOutlined />
            统一身份认证服务
          </Menu.Item>
          <Menu.Item key='setting'>
            <SettingOutlined />
            用户设置
          </Menu.Item>
          <Menu.Divider />
        </>
      )}

      <Menu.Item key='logout'>
        <LogoutOutlined />
        {formatMessage({ id: 'component.globalHeader.avatarDropdown.logout' })}
      </Menu.Item>
    </Menu>
  );
  // console.log('currentUser', currentUser);
  return currentUser && currentUser.userName ? (
    <HeaderDropdown overlay={menuHeaderDropdown} placement='bottomCenter'>
      <span className={`${styles.action} ${styles.account}`}>
        <span className={styles.name}>{currentUser.userName}</span>
      </span>
    </HeaderDropdown>
  ) : null;
}

export default connect(({ user }) => ({
  currentUser: user.currentUser,
}))(AvatarDropdown);
